﻿using Expect.Core.Interfaces;

namespace Expect.Core.Helpers
{
    /// <summary>
    /// Methods for fixture resolution
    /// </summary>
    public static class ResolveFixtureHelpers
    {
        /// <summary>
        /// Resolve the fixture from parameters
        /// </summary>
        public static void Resolve(this IExecutionContext context, FixtureDefinition fixture)
        {
            if (fixture.ClassType == null && string.IsNullOrEmpty(fixture.ClassName))
            {
                context.AsFixtureClass(fixture);

                if (fixture.Parent.HasClass)
                {
                    context.AsMethodWithFixtureData(fixture.Parent.ClassType, fixture);
                }
                if (context.Container != null)
                {
                    context.AsMethodWithFixtureData(context.Container.GetType(), fixture);
                }
            }

            if (fixture.ClassType == null && !string.IsNullOrEmpty(fixture.ClassName))
            {
                fixture.ClassType = context.Catalog.GetFixture(fixture.ClassName);                
            }

            if (fixture.ClassType != null && !fixture.HasImplementation && !context.ResolveOnly)
            {
                fixture.InitializeImplementation(context);
            }

            if (fixture.HasImplementation)
            {
                fixture.Implementation.Resolve();
            }
        }
    }
}
